package com.tz.actions;

import com.opensymphony.xwork2.ActionSupport;
import com.tz.dao.CategoryDao;
import com.tz.entity.Category;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * 处理分类列表
 */
@Namespace("/category")
@ParentPackage("json-default")
public class CategoryAction extends ActionSupport {

    private static final long serialVersionUID = -340146035132582190L;

    @Autowired
    private CategoryDao categoryDao;

    private List<Category> result;

    @Action(value = "list", results = {
            @Result(type = "json", params = {"root", "result"})
    })
    public String list() {
        result = categoryDao.getByLevel(0);
        for (Category c : result) {
            for (Category c2 : c.getChildren()) {
                c2.setChildren(null);
            }
        }
        return SUCCESS;
    }

    public List<Category> getResult() {
        return result;
    }

    public void setResult(List<Category> result) {
        this.result = result;
    }
}
